<?php

namespace app\ypz_forum_admin\controller;


use app\BaseController;
use think\Facade\Db;
use think\facade\View;

class Deal extends BaseController
{
    //后台 R2103010231
    public function mesdeal()
    {
        $sid     = $this->request->get('sid', 1);
        $where   = [];
        $where[] = ['section_id', '=', $sid];
        $keyword = $this->request->get('keyword');
        if ($keyword) {
            $where[] = ['title', 'like', '%'.$keyword.'%'];
            
        }
        //dump($where);
        // 验证登录
        $this->check_login();
        $section = Db::name('section')->select();
        $article = Db::name('article')
            ->where($where)
            ->select();
        //dump($section);
        // 视图查询:R2103010231
        return view('mesdeal',
            ['section' => $section, 'article' => $article, 'keyword' => $keyword, 'sid' => $section[$sid]['name']]);
    }
    
    //后台 R2103010231
    public function adetail()
    {
        $this->check_login();
        $id      = $this->request->get('id');
        $article = Db::name('article')->where('id', $id)->select();
        $mes     = Db::name('mes')->where('article_id', $id)->select();
        return view('', ['article' => $article, 'mes' => $mes]);
        
    }
    
    //后台 R2103010231
    public function resdel()
    {
        $id = $this->request->get('id');
        $this->check_login();
        $resd = Db::name('mes')->where('id', $id)->delete();
        if ($resd == 1) {
            //后台 R2103010231
            $this->success('成功删除回复!', 'deal/mesdeal');
        } else {
            //后台 R2103010231
            $this->error('删除回复失败!', 'deal/mesdeal');
        }
    }
    
    public function mesdel()
    {
        $id   = $this->request->get('id');
        $mesd = Db::name('article')->where('id', $id)
            ->delete();
        if ($mesd == 1) {
            //修改成功R2103010231
            $this->success('成功删除贴子！', 'deal/mesdeal');
        } else {
            //修改失败R2103010231
            $this->error('删除贴子失败！', 'deal/mesdeal');
        }
    }
    
    public function userdeal()
    {
        $user = Db::name('user')->select();
        return view('', ['user' => $user]);
    }
    
    public function userpower()
    {
        $uid  = $this->request->get('uid');
        $user = Db::name('user')->find($uid);
        return view('', ['user' => $user]);
    }
    
    // 板块查看R2103010231
    public function secdeal()
    {
        $section = Db::name('section')->select();
        
        //halt($section);
        return View::fetch('', ['sec' => $section]);
    }
    
    // 模块添加R2103010231
    public function secadd()
    {
        return View::fetch();
        
    }
    
    public function secdoadd()
    {
        // 提取表单元素的值
        $uSec = $this->request->post('nsec', '', 'trim');
        // 查询数据，问一下，这个账号能否重复
        $re = Db::name('section')
            ->whereOr('name', $uSec)
            ->find();
        // 如果账号已经重复了，就停止注册
        if ($re != null) {
            $this->error('这个版块名称已经被占用！');
        }
        // 添加数据
        $data  = [
            'name' => $uSec,
        ];
        $uSec1 = Db::name('section')
            ->insert($data);
        if ($uSec1 == 1) {
            #注册成功
            $this->success('版块添加成功！', 'deal/secdeal');
        } else {
            #注册失败
            $this->error('版块添加失败，请稍后再试！', 'deal/secdeal');
        }
        
    }
    
    // 删除版块R2103010231
    public function secdel(int $sid)
    {
        $secd = Db::name('section')->where('id', $sid)->delete();
        if ($secd == 1) {
            #修改成功
            $this->success('成功删除版块！', 'deal/secdeal');
        } else {
            #修改失败
            $this->error('删除版块失败！', 'deal/secdeal');
        }
        
    }
    
    // 轮播图版块R2103010231
    public function boxlist()
    {
        $adv = Db::name('adv')->select();
        //dump($section);
        // 视图查询:R2103010231
        return view('', ['adv' => $adv]);
    }
    
    public function addbox()
    {
        return view();
    }
    
    //R2103010231 添加轮播图
    public function addboxok()
    {
        // 提取表单元素的值
        $uSec = $this->request->post('nsec', '', 'trim');
        $re = Db::name('adv')
            ->whereOr('name', $uSec)
            ->find();
        // 如果账号已经重复了，就停止注册
        if ($re != null) {
            $this->error('这个轮播图名称已经被占用！');
        }
        // 添加数据
        $data  = [
            'name' => $uSec,
        ];
        $uSec1 = Db::name('section')
            ->insert($data);
        if ($uSec1 == 1) {
            #注册成功
            $this->success('轮播图添加成功！');
        } else {
            #注册失败
            $this->error('轮播图添加失败，请稍后再试！');
        }
        
    }
    
    //R2103010231 停止轮播图
    public function stopbox($id)
    {
        Db::name('adv')->where('id',$id)->update(['status',1]);
    
        $this->success('轮播图暂停成功！');
        
    }
    
    //R2103010231 删除轮播图
    public function delbox()
    {
        //Db::name('adv')->delete($id);
        $this->success('轮播图删除成功！');
        
    }
}
